<!-- 当前组件名： component -->
<template>
  <div class='news' v-loading="loading">
    <div class='news-container'>
      <div class='aside-left'>
        <cfCard title='' class='page-container-search'>
          <div slot='content'>
            <el-input placeholder='输入关键字进行过滤' class="right12 bottom12" size='small' :style="hasPermiOr(['system:newsType:add'])?{} : {width: '100%'}" v-model='filterText'></el-input>
            <span class="el-icon-circle-plus-outline link-span" v-hasPermi="['system:newsType:add']" style="font-size: 18px;" @click="handleAddTree"></span>
            <el-tree ref='tree' :data='treeData' :props="{label: 'typeName'}" node-key='typeId' empty-text='暂无数据' default-expand-all :expand-on-click-node='false' :filter-node-method='filterNode' highlight-current @node-click='nodeClick'>
              <span class="custom-tree-node" slot-scope="{ node, data }">
                <span><svg-icon class="svgIcon" icon-class="cascader" style="margin-right: 6px;"></svg-icon>【{{data.publicFlag == 'Y' ? '公有' : '私有'}}】{{ node.label }}</span>
                <span class="operate-btn">
                  <el-button type="text" v-if="data.isDefault !='Y'" v-hasPermi="['system:newsType:edit']" size="mini" @click.stop="() => handleEditTree(data)" class="el-icon-edit"></el-button>
                  <el-button type="text" v-if="data.isDefault != 'Y'" v-hasPermi="['system:newsType:delete']" size="mini" @click.stop="() => handleRemoveTree(data)" class="el-icon-delete-solid"></el-button>
                </span>
              </span>
            </el-tree>
          </div>
        </cfCard>
      </div>
      <div class='aside-right' :style="device=='mobile'?{minWidth: '100%'}:{}">
        <cfCard title='' class='page-container-search'>
          <el-form slot='content' class='searchArea' :model='searchParams' ref='searchParams' label-width='60px' inline size='small'>
            <el-form-item label='标 题' prop="title">
              <el-input v-model='searchParams.title' placeholder='请输入标题'></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size='mini' type='primary' @click='handleSearch'>查询</el-button>
              <el-button size='mini' @click='handleReset'>重置</el-button>
            </el-form-item>
          </el-form>
        </cfCard>
        <div class='bottom12'></div>
        <cfCard title='列表' hasTable :tableObj='tableDataObj' class='news-container-bottom'>
          <!--表格区域-->
          <template slot='btn'>
            <el-button size='mini' type='primary' v-hasPermi="['system:news:add']" @click='handleAdd'>新增</el-button>
          </template>
          <template slot='content'>
            <cfTable ref='cfTable' :tableDataObj='tableDataObj' :tableData='tableData'>
              <template slot='typeId'>
                <el-table-column label='信息类型'>
                  <span slot-scope='{row}'>{{ getNoticesTypeName(row.typeId)}}</span>
                </el-table-column>
              </template>
              <template slot='state'>
                <el-table-column label='信息状态'>
                  <span slot-scope='{row}'>{{ row.state == 2 ? '已结束' : (row.state == 1 ? '发布中' : '未发布')}}</span>
                </el-table-column>
              </template>
              <template slot='operate'>
                <el-table-column label='操作' width='100px' align='right' v-if="hasPermiOr(['system:news:edit','system:news:detail','system:news:delete'])">
                  <span slot-scope='{row}'>
                    <el-button v-if="row.createBy == userId" v-hasPermi="['system:news:edit']" type='text' size='small' @click="handleEdit(row, 'edit')">编辑</el-button>
                    <el-button v-else type='text' size='small' v-hasPermi="['system:news:detail']" @click="handleEdit(row, 'detail')">详情</el-button>
                    <el-button type='text' size='small' class='delete-span' v-hasPermi="['system:news:delete']" @click='handleDelete(row)'>删除</el-button>
                  </span>
                </el-table-column>
              </template>
            </cfTable>
            <pagination :total='queryParams.total' :page.sync='queryParams.pageNum' :limit.sync='queryParams.pageSize' @pagination='getListFn'/>
          </template>
        </cfCard>
      </div>
    </div>
    <cfPageDialog v-if='pageDialogObj.visible' :pageDialog='pageDialogObj'>
      <updateNotice v-if='pageDialogObj.visible' :pageDialogObj='pageDialogObj' @refresh="getListFn"></updateNotice>
    </cfPageDialog>
    <updateTypeDialog v-if="dialogObj.visible" :dialogObj="dialogObj" @refreshTree="getTreeList"></updateTypeDialog>
  </div>
</template>

<script>
import news from '@/api/news'
import updateTypeDialog from './updateTypeDialog.vue'
import updateNotice from './updateNotice.vue'
export default {
  name: 'componentName',
  components: {updateTypeDialog, updateNotice },
  data() {
    return {
      filterText: '',
      treeData: [],
      tableDataObj: {
        id: 'noticesId',
        showSelection: false,
        showIndex: true,
        defaultExpandAll: true,
        treeProps: {children: 'children', hasChildren: 'hasChildren'},
        option: [
          {id: 1, label: '标题', prop: 'title', show: 1, needSlot: false},
          {id: 2, label: '信息类型', prop: 'typeId', show: 1, needSlot: true},
          {id: 3, label: '信息状态', prop: 'state', show: 1, needSlot: true},
          {id: 4, label: '发布时间', prop: 'startTime', show: 1, needSlot: false},
          {id: 5, label: '结束时间', prop: 'endTime', show: 1, needSlot: false},
        ]
      },
      tableData: [],
      dialogObj: {
        visible: false,
        id: null,
        title: '新增',
        width: '538px',
        component: ''
      },
      pageDialogObj: {
        visible: false,
        id: null,
        title: '信息公告',
        type: ''
      },
      searchParams: {
        typeId: '',
        communityName: '',
        communityName1: '',
      },
      queryParams: {
        total: 0,
        pageNum: 1,
        pageSize: 10
      },
      loading: true
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  computed: {
    userId() {
      return this.$store.getters.userId;
    },
    device() {
      return this.$store.getters.device;
    }
  },
  mounted() {
    this.getTreeList()
  },
  methods: {
    /**新增新闻类型按钮事件 */
    handleAddTree() {
      this.dialogObj.visible = true;
      this.dialogObj.id = '';
      this.dialogObj.width = '428px';
      this.dialogObj.title = '新增类型';
      this.dialogObj.component = 'updateTypeDialog';
    },
    handleEditTree(data) {
      this.dialogObj.visible = true;
      this.dialogObj.id = data.typeId;
      this.dialogObj.width = '428px';
      this.dialogObj.title = '编辑类型';
      this.dialogObj.component = 'updateTypeDialog';
    },
    handleRemoveTree(data) {
      this.$confirm(`是否确认删除【${data.typeName}】类型数据`, '提示', {
        cancelText: '取消',
        confirmText: '确定'
      }).then(async ()=>{
        this.loading = true;
        this.$message.success('删除成功');
        this.getTreeList();
        setTimeout(() => {
          this.loading = false;
        }, 500);
      }).catch(()=>{})
    },
    /**树节点点击事件 */
    nodeClick(data) {
      this.searchParams.typeId = data.typeId;
      this.handleSearch();
    },
    /**树节点过滤筛选 */
    filterNode(value, data) {
      if (!value) return true;
      return data.typeName.indexOf(value) !== -1;
    },
    /**获取左侧树结构*/
    async getTreeList() {
      this.loading = true;
      this.treeData = [
        {
          communityId: "000000",
          isDefault: "Y",
          orderNum: "-2",
          publicFlag: "Y",
          typeId: "852023080160930001",
          typeName: "新闻动态",
        },
        {
          communityId: "000000",
          isDefault: "Y",
          orderNum: "-2",
          publicFlag: "Y",
          typeId: "852023080137140002",
          typeName: "新闻动态",
        }
      ];
      // this.$nextTick(()=>{
      //   this.$refs.tree.setCurrentKey(this.treeData[0].typeId);
      //   this.nodeClick(this.treeData[0]);
      // })
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    /**获取列表数据方法*/
    async getListFn() {
      this.loading = true;
      this.tableData = [
        {
          endTime: "2024-12-30 16:00:00",
          fileId: "202023101399720627",
          filePath: "https://whzsoss.oss-cn-shanghai.aliyuncs.com/2023/10/13/c8672918d4c04dd09eece26891eec2aa.png",
          noticesId: "992023101311380634",
          publishName: "中实物业",
          searchValue: null,
          shareFlag: "Y",
          startTime: "2023-10-12 16:00:00",
          state: "1",
          title: "党建引领管理赋能 助推服务提质增效",
          typeId: "852023080160930001",
        }
      ];
      this.queryParams.total = 1;
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    getNoticesTypeName(val) {
      let str = '';
      this.treeData.map(item=>{
        if(item.typeId == val) {
          str = item.typeName;
          return;
        }
      })
      return str;
    },
    /**新增列表数据方法*/
    handleAdd() {
      this.pageDialogObj.visible = true;
      this.pageDialogObj.id = '';
      this.pageDialogObj.type = '';
    },
    /**编辑列表数据方法*/
    handleEdit(obj, type) {
      this.pageDialogObj.visible = true;
      this.pageDialogObj.id = obj.noticesId;
      this.pageDialogObj.type = type;
    },
    /**删除列表数据方法*/
    handleDelete(obj) {
      this.$confirm('确定要删除当前数据吗？', '提示', {
        cancelText: '取消',
        confirmText: '确定'
      }).then(async ()=>{
        this.loading = true;
        this.$message.success('删除成功');
        this.handleSearch();
        setTimeout(() => {
          this.loading = false;
        }, 500);
      }).catch(()=>{})
    },
    /**查询按钮事件*/
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.queryParams.total = 0;
      this.getListFn();
    },
    /**重置按钮事件*/
    handleReset() {
      this.$refs.searchParams.resetFields();
      this.handleSearch();
    },
  },
}
</script>

<style lang='scss' scoped>
.news {
  height: 100%;
  overflow-y: overlay;
  .news-container {
    height: 100%;
    display: flex;
    .aside-left {
      width: 300px;
      height: 100%;
      flex-shrink: 0;
      margin-right: 12px;
      background: #fff;
      overflow: overlay;
      ::v-deep .el-tree-node__expand-icon {
        display: none !important;
      }
      ::v-deep .custom-tree-node {
        width: 100%;
        display: flex;
        justify-content: space-between;
        font-size: 14px;
        align-items: baseline;
        padding-right: 6px;
        
        .operate-btn {
          display: none;
          .el-icon-circle-plus-outline, .el-icon-delete-solid {
            font-size: 16px;
          }
        }
        &:hover {
          .operate-btn{
            display: block;
          }
        }
      }
    }
    .aside-right {
      width: 100%;
      height: 100%;
      overflow: hidden;
      overflow-y: overlay;
    }
    
  }
}
</style>